package interceptor

import (
	"DuckShop/internal/user/global"
	"context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

// GrpcPanicInterceptor rpc异常拦截器
func GrpcPanicInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
	var err error
	defer func() {
		if r := recover(); r != nil {
			global.DLog.Sugar().Errorf("Recovered from panic: %v", r)
			err = status.Errorf(codes.Internal, "Internal server error: %v", err)
		}
	}()
	resp, err := handler(ctx, req)
	if err != nil {
		err = status.Errorf(codes.Internal, "Internal server error: %v", err)
	}

	return resp, err
}
